package net.net23.sysdent.server.jdo.control;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.net23.sysdent.server.jdo.pojos.User;

public class UserController {

	@SuppressWarnings("unchecked")
	public static User getUserByMail(String mail) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		Query query = persistenceManager.newQuery(User.class);
		query.setFilter("mail == mailParam");
		query.declareParameters("String mailParam");
		List<User> list = (List<User>) query.execute(mail);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public static User forceUserByMail(String mail) {
		User user = UserController.getUserByMail(mail);
		if (user == null) {
			user = UserController.save("", mail);
		}
		return user;
	}

	public static User save(String string, String mail) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User user = new User("", mail);
		pm.makePersistent(user);
		return UserController.getUserByMail(mail);
	}
}
